package cn.lf.service.impl;


import cn.lf.constant.ResponseCodeEnum;
import cn.lf.dto.sys.dict.SysDictTypeInfoDTO;
import cn.lf.dto.sys.dict.SysDictTypeRepDTO;
import cn.lf.dto.sys.dict.SysDictTypeReqDTO;
import cn.lf.dto.sys.dict.SysDictTypeSaveDTO;
import cn.lf.entity.SysDictData;
import cn.lf.entity.SysDictType;
import cn.lf.exception.SelfException;
import cn.lf.mapper.SysDictDataMapper;
import cn.lf.mapper.SysDictTypeMapper;
import cn.lf.service.ISysDictTypeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

/**
 * <p>
 * 字典类型 服务实现类
 * </p>
 *
 * @author guoyy
 * @since 2024-04-03
 */
@Service
public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper, SysDictType> implements ISysDictTypeService {

    @Resource
    private SysDictDataMapper sysDictDataMapper;

    @Override
    public IPage<SysDictTypeRepDTO> queryDictTypePage(SysDictTypeReqDTO params) {

        Page<SysDictTypeRepDTO> page = new Page<>(params.getCurrentPage(),params.getPageSize());

        return baseMapper.selectDictTypePage(page,params);
    }

    @Override
    public SysDictTypeInfoDTO getSysDictTypeById(String dictTypeId) {

        SysDictTypeInfoDTO result = new SysDictTypeInfoDTO();

        SysDictType sysDictType = baseMapper.selectById(dictTypeId);

        if (Objects.nonNull(sysDictType)){
            BeanUtils.copyProperties(sysDictType,result);
        }else {
            return null;
        }

        return result;
    }

    @Override
    public void saveSysDictType(SysDictTypeSaveDTO sysDictTypeSaveDTO) {

        if (Objects.nonNull(sysDictTypeSaveDTO.getDictTypeId())&&!Objects.equals(sysDictTypeSaveDTO.getDictTypeId(),"")){

            SysDictType sysDictType = new SysDictType();

            BeanUtils.copyProperties(sysDictTypeSaveDTO,sysDictType);

            baseMapper.updateById(sysDictType);

        }else {

            SysDictType sysDictType = new SysDictType();

            BeanUtils.copyProperties(sysDictTypeSaveDTO,sysDictType);

            sysDictType.setDictTypeId(UUID.randomUUID().toString());

            baseMapper.insert(sysDictType);

        }


    }

    @Override
    public void delSysDictType(String dictTypeId) {

        LambdaQueryWrapper<SysDictData> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        lambdaQueryWrapper.eq(SysDictData::getDictTypeId,dictTypeId);

        List<SysDictData> dictDataList = sysDictDataMapper.selectList(lambdaQueryWrapper);

        if (!dictDataList.isEmpty()){
            throw new SelfException(ResponseCodeEnum.SYS_ERROR.getCode(),"该类型底下有字典数据");
        }

        baseMapper.deleteById(dictTypeId);

    }
}
